// swf_header.cpp : swf_header
// 2009-10

#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>

struct swf_header_t {
	unsigned char signature[3];
	unsigned char version;
	uint32_t length;

public:

	void dump( void ) {
		printf( "signature [%c%c%c]\n", signature[0], signature[1], signature[2] );
		printf( "version [%d]\n", (int) version );
		printf( "length [%u]\n", length );
	}
};

static int read_swf_header( FILE *fp, struct swf_header_t &header );

int main( int argc, char *argv[] )
{
	if( argc < 2 ) {
		fprintf( stderr, "need swf file\n" );
		exit( EXIT_FAILURE );
	}

	const char *swf_file_path = argv[1];

	FILE *fp = fopen( swf_file_path, "r" );
	if( NULL == fp ) {
		fprintf( stderr, "open file %s failed\n", swf_file_path );
		exit( EXIT_FAILURE );
	}

	struct swf_header_t header;
	int rc = read_swf_header( fp, header );
	if( 0 != rc ) {
		fprintf( stderr, "bad data format %s\n", swf_file_path );
		exit( EXIT_FAILURE );
	}

	header.dump();

	return 0;
}

int read_swf_header( FILE *fp, struct swf_header_t &header )
{
	if( fread( &header, sizeof( header ), 1, fp ) != 1 )
		return -1;
	return 0;
}

